/**
 * 文  件  名：  UserInfoDaoImpl.java
 * 工  程  名：  7You
 * 创建日期：  2013-10-13
 * 创建作者：  杨  强
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IUserInfoDao;
import com.qiyou.entity.UserInfo;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 用户信息数据操作实现类
 * 
 * @author 杨 强
 * 
 */
public class UserInfoDaoImpl implements IUserInfoDao {
    private Connection conn = null;

    public UserInfoDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doUpdate(UserInfo userInfo) throws SQLException {
        boolean result = false;
        String sql = "update `tb_user_info` set `userID` = ?, `role` = ?, `nickname` = ?, `userName` = ?, `sex` = ?, `age` = ?, `userFace` = ?, `tel` = ?, `qq` = ?, `email` = ?, `userLocal` = ?, `userIntro` = ?, `regDate` = ? where `userID` = ?";

        Object[] values = TransformUtil.beanTOArray(userInfo, false);
        values[12] = new Timestamp(userInfo.getRegisterDate().getTime());

        result = DBUtil.execute(conn, sql, values, values[0]);
        return result;
    }

    @Override
    public UserInfo getUserInfo(String userID) throws SQLException {
        UserInfo result = null;
        String sql = "select * from `tb_user_info` where `userID` = ?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = (UserInfo) TransformUtil.arrayToBean(all.get(0),
                    UserInfo.class);
        }
        return result;
    }

    @Override
    public List<UserInfo> getUserInfo(Object... params) throws SQLException {
        List<UserInfo> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_user_info` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (all.size() > 0) {
            result = new ArrayList<UserInfo>(all.size());
            for (Object[] objects : all) {
                UserInfo userInfo = new UserInfo();

                userInfo = (UserInfo) TransformUtil.arrayToBean(objects,
                        UserInfo.class);

                result.add(userInfo);
            }
        }
        return result;
    }

}
